package com.lnsf.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lnsf.dao.UserDao;
import com.lnsf.entity.User;
import com.lnsf.service.IUserService;
import com.lnsf.util.MailUtil;

/*
 * UserService 实现类UserServiceImpl
 * author:黄华炎
 * 2017-07-26
 * 
 * 
 */

@Service
@Transactional
public class UserServiceImpl implements IUserService {

	@Autowired
	private UserDao userDao;
	
	//注册
	public String register(User user ) {
		
		User userdetail = userDao.save(user);
		if(userdetail.getPhone() != null){
			new Thread((Runnable) new MailUtil(userdetail.getEmail(), userdetail.getCode())).start();
			return "success";
		}
		else
			return "error";
	}
	
	//登陆
	public User login(User user) {
		User loginUser = userDao.findByPhoneAndPassword(user.getPhone(), user.getPassword());
		return loginUser;
	}
	
	@Override
	public User getUserByPhone(String phone) {
		User userdetail = userDao.findByPhone(phone);
		return userdetail;
	}

	
	@Override
	public Iterable<User> getUserByPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);//分页核心代码
		return  userDao.findAll();
	}

	//模糊查询
	@Override
	public List<User> getUserByCondition(User user) {
		
		List<User> list= new ArrayList<User>();
		
		if(user.getUsername() == null && user.getUsername().equals("")){
			User user_Phone = userDao.findByPhone(user.getPhone());
			list.add(user_Phone);
		}
		else {
			list = userDao.findByUsernameLike(user.getUsername());
		}
		
		return list;
	}

	//删除用户
	@Override
	public void deleteUser(Integer userid) {
		User user = userDao.findOne(userid);
		userDao.delete(user);
	}

	//更新用户
	@Override
	public void updateUser(User user) {
		//userDao.updateUser(username, userid);
		
	}

	//获得全部用户的数量
	@Override
	public Long getTotalUser() {
		// TODO Auto-generated method stub
		return userDao.count();
	}
	
	//修改用户激活状态
	public Integer activeUser(String code){
		
		return userDao.activeUser(code);
		
		
	}

	
	
}
